from flask_mail import Mail
from flask import current_app
from threading import Thread


class BaseMail(Mail):
    def async_send_mail(self, message):
        # current_app._get_current_object() 返回当前app
        thread = Thread(target=self._send_async_mail, args=[current_app._get_current_object(), message])
        thread.start()

    def _send_async_mail(self, app, message):
        with app.app_context():
            self.send(message)
